from ..config.config import python,workflow_dir,pub_dir
from pathlib import Path
from datetime import datetime
import time
def dealPipelineSell(samplesheet,runid,shellDir,outdir,fqDir,sextype,runbwa,runkraken,runrgi,runmyco,runkkbwa,runntbwa,mergekraken,sex):
  # 将shell脚本路径存入变量
  
  
  
  date = datetime.strftime(datetime.today(),'%Y%m%d')
  # t = time.localtime()
  # year = str(t.tm_year)[2:]
  # date = f'{year}{t.tm_mon:02}{t.tm_mday:02}'
  # date = str(outdir).split('/')[-1].split('_')[0]
  # ods = str(outdir).split('/')[-1].split('_')
  # jobid = ods[1] + '_' + ods[3][1:]
  jobid = runid
  
  # pipeline.sh 写入命令
  with open(shellDir / Path('pipeline.sh'), 'w') as pl:
    pl.write(f'''\
  # 创建网页系统服务器数据存放路径
  cd {outdir} && mkdir -p {pub_dir}{runid} && mkdir -p {pub_dir}{runid}/rgi {pub_dir}{runid}/nt {pub_dir}{runid}/ra {pub_dir}{runid}/kkbwa {pub_dir}{runid}/list && mkdir -p {pub_dir}{runid}/ntbwa
  #nohup {python} {workflow_dir}tools/monitorBlastEnd4GZ.py -id {outdir} -ss {samplesheet} &
  #nohup {python} {workflow_dir}tools/monitorRABGI.py {samplesheet} {outdir} {sextype} {fqDir}/{runid}.fastq.Stats.xls & \n\n''')

          # bcl2fastq sh
          # bcl2fastq = shellDir / Path('p1.bcl2fastq.sh')
          # with open(bcl2fastq, 'w') as b2f:
          #     b2f.write(f'bcl2fastq -R {bclDir} --sample-sheet {samplesheet} -o {fqDir} -p 72 --no-lane-splitting --barcode-mismatches 0 --minimum-trimmed-read-length 50 \n')
    pl.write(f'''
  # 合并多条lane中的相同样本
  {python} {workflow_dir}parallel/qsubSH.py {outdir}/L01/work.sh 20

  # 根据simplesheet和cnv_stat.xls生成cnv.Stats.xls和cnv.Stats.html S200040707.fastq.Stats.xls和S200040707.fastq.Stats.html
  {python} {workflow_dir}qcstat/fastqStat4gz.py {samplesheet} {fqDir}/SequenceStat.txt {fqDir}/{runid}

  # 生成/fastq/cnv_stat.txt
  {python} {workflow_dir}cnv_stat.py  {samplesheet} /data/share/{runid}/L01/SequenceStat.txt {outdir}

  # 生成.cnv.Stats.html
  {python} {workflow_dir}qcstat/cnvStat4gz.py {samplesheet} {fqDir}/cnv_stat.txt {outdir}/fastq/{runid}

  # 拷贝数据至网页系统服务器路径
  #nohup {python} {workflow_dir}mngsTime.GZBGI.py {runid} &
  cp {fqDir}/{runid}.fastq.Stats.xls /home/zhengzhiqiang/today/guangzhou \n\n''')
    pl.write(f'''# run kraken && qsub nt
  # 去除各种生物比对上的部分
  {python} {workflow_dir}parallel/qsubSH.py {runbwa} 7

  # kraken2物种注释
  {python} {workflow_dir}parallel/qsubSH.py {runkraken} 7

  # 耐药基因
  {python} {workflow_dir}parallel/qsubSH.py {runrgi} 2 \n\n''')
    pl.write(f'''# run merge and check sex
  sh {mergekraken}
  nohup {python} {workflow_dir}historycheck/historycheckGZ.py {outdir} {jobid} &
  {python} {workflow_dir}parallel/qsubSH.py {sex} 36
  cat {outdir}/*/*.sex.xls | sed 1i\ "sample\\tautosomesMean\\tautosomesMedian\\tChrX\\tChrY\\tprogram2vote" > {outdir}/all.sex.xls
  {python} {workflow_dir}parallel/qsubSH.py {runmyco} 7
  {python} {workflow_dir}parallel/qsubSH.py {runntbwa} 3 
  {python} {workflow_dir}parallel/qsubSH.py {runkkbwa} 3
  {python} {workflow_dir}qcstat/fastqc.py {fqDir}
  {python} {workflow_dir}qcstat/Q30BGI.py {fqDir}
  cat {fqDir}/*.q30 > {fqDir}/q30.stat.xls

  # 
  {python} {workflow_dir}qcstat/runStat.py {outdir} {sextype}
  {python} {workflow_dir}getNCList.py {outdir} {pub_dir}{runid}
  /data/mngsSYS/b/venv/bin/python {workflow_dir}parallel/SampleQstatUpdate.py {outdir}/runStat.xls {jobid}
  #/data/mngsSYS/b/venv/bin/python {workflow_dir}parallel/JobSampleUpdate.py {outdir}/GZ.mNGS.{date}.mergeKK.xlsx GZ {jobid}
  /data/mngsSYS/b/venv/bin/python {workflow_dir}parallel/autoGZ.py {pub_dir}{runid} KK && cd /home/zhengzhiqiang/today/guangzhou && tar zcvf {runid}.tar.gz {runid}
  nohup {python} {workflow_dir}updateAuto.py /home/zhengzhiqiang/today/.auto/guangzhou/{runid}/output {jobid} &
  nohup {python} {workflow_dir}list.py /home/zhengzhiqiang/today/guangzhou {runid} &
  /data/mngsSYS/b/venv/bin/python {workflow_dir}parallel/updateJobStatus.py {jobid}
  cp {outdir}/runStat.xls {pub_dir}{runid}/ && cp {outdir}/*/*.litesam {pub_dir}{runid}/kkbwa && cp {outdir}/*/*.litentsam {pub_dir}{runid}/ntbwa/  && cd /home/zhengzhiqiang/today/guangzhou && tar zcvf {runid}.tar.gz {runid} && rm {pub_dir}{runid}/kkbwa/*YF-*  && rm {pub_dir}{runid}/ntbwa/*YF-* 
  {python} {workflow_dir}generate_QC.py {jobid} 
  {python} {workflow_dir}mngsTime.GZBGI.py {jobid} 
  {python} {workflow_dir}qcstat/check_q30.py {outdir}/runStat.xls
  {python} {workflow_dir}parallel/qsubSH.py {outdir}/shell/all.cnv.sh 2
  #echo {jobid}  > /data/tmp/Cal/workdir/list
  sh {workflow_dir}parallel/work.sh  \n''')